package greedy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 763. 划分字母区间
 * User: Li_yizYa
 * Date: 2025/6/27
 * Time: 10:33
 */
public class Solution14 {
    public List<Integer> partitionLabels(String s) {
        List<Integer> ret = new ArrayList<>();
        if (s == null || s.length() == 0) {
            return ret;
        }
        int[] hash = new int[26];
        for (int i = 0; i < s.length(); i++) {
            hash[s.charAt(i) - 'a'] = i;
        }
        int start = 0;
        int tmp = hash[s.charAt(0) - 'a'];
        for (int i = 0; i < s.length(); i++) {
            if (i == tmp) {
                ret.add(i - start + 1);
                start = i + 1;
                if (start < s.length()) {
                    tmp = hash[s.charAt(start) - 'a'];
                }
            } else {
                tmp = Math.max(tmp, hash[s.charAt(i) - 'a']);
            }
        }
        return ret;
    }
}
